package com.gitee.wsl.flow.create.datasource

import com.gitee.wsl.flow.create.datasource.request.Request
import com.gitee.wsl.result.sample.ProcessResult
import kotlinx.coroutines.flow.Flow

typealias DataState<T> = ProcessResult<T, Throwable, DataSourceLoadState>


interface FlowDataSource<R : Request, T> {
    /**
     * Send a read request to the Datasource
     */
    fun read(request: R): Flow<DataState<T>>

    /**
     * Save data to the datasource
     */
    suspend fun save(request: R, data: T?)

    /**
     * Delete data in the datasource
     */
    suspend fun delete(cacheableId: String)

    /**
     * Clear all data in the datasource
     */
    suspend fun clear()
}

enum class DataSourceLoadState{

    IDLE, LOADING;

    companion object{

        fun <S> of(value:DataSourceLoadState) = DataState.StateValue(value)
        
        fun <S> idle() = of<S>(IDLE)

        fun <S> loading() = of<S>(LOADING)
        
    }

}